﻿// -----------------------------------------------------------------------
// <copyright file="CommonImpl.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Collections.Generic;
using System.Data.Common;
using StampController.BL.Models;
using StampController.Core;

namespace StampController.BL
{
    /// <summary>
    ///     TODO: Update summary.
    /// </summary>
    public class CategoryImpl
    {
        public IList<CategoryModel> GetCategoryList()
        {
            var list = new List<CategoryModel>();
            using (var dbHelper = new DbHelper())
            {
                using (DbDataReader reader = dbHelper.ExecuteReader("SELECT CategoryId, CategoryName FROM Category"))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            list.Add(new CategoryModel
                            {
                                CategoryId = reader.Get<int>(0),
                                CategoryName = reader.GetString(1)
                            });
                        }
                    }

                    reader.Close();
                }

                return list;
            }
        }

        public CategoryModel GetCategory(int categoryId)
        {
            CategoryModel category = null;
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                {
                    {"@CategoryId", categoryId}
                };
                using (DbDataReader reader = dbHelper.ExecuteReader("SELECT CategoryId, CategoryName, HODComplete FROM Category WHERE CategoryId = @CategoryId", parameter))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        category = new CategoryModel
                        {
                            CategoryId = reader.Get<int>(0),
                            CategoryName = reader.GetString(1),
                            HODComplete = reader.Get<bool>(2)
                        };
                    }

                    reader.Close();
                }
            }

            return category;
        }

        public CategoryModel GetCategory(string requestId)
        {
            CategoryModel category = null;
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                {
                    {"@RequestId", requestId}
                };
                using (DbDataReader reader = dbHelper.ExecuteReader("SELECT CategoryId, CategoryName, HODComplete FROM Category WHERE CategoryId = (SELECT TOP 1 CategoryId FROM RequestForm WHERE RequestId = @RequestId AND LastApprovedDate IS NULL)", parameter))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        category = new CategoryModel
                        {
                            CategoryId = reader.Get<int>(0),
                            CategoryName = reader.GetString(1),
                            HODComplete = reader.Get<bool>(2)
                        };
                    }

                    reader.Close();
                }
            }

            return category;
        }
    }
}